package com.kaishengit.web;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.kaishengit.pojo.Issues;
import com.kaishengit.pojo.Message;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.Talk;
import com.kaishengit.service.IssuesService;
import com.kaishengit.service.MessageService;
import com.kaishengit.service.MilestoneService;
import com.kaishengit.service.ProjectService;
import com.kaishengit.service.TalkService;
import com.kaishengit.service.UserService;
import com.kaishengit.util.Const;
import com.kaishengit.util.DateUtil;

@Results({@Result(name=BaseAction.RELOAD,location="issues!list.action",type="redirectAction",params={"id","${id}"})})
public class IssuesAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	@Resource
	private ProjectService projectService;
	@Resource
	private IssuesService issuesService;
	@Resource
	private MilestoneService milestoneService;
	@Resource
	private TalkService talkService;
	@Resource
	private UserService userService;
	@Resource
	private MessageService messageService;
	
	
	
	private Project project;
	private Integer projectid;
	private Integer type;
	private Issues issues;
	private Integer milestoneid;
	private Integer id;
	private Talk talk;
	private String state;
	private Integer userid;
	private Message message;
	
	

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	public String input() throws Exception {
		project = projectService.findById(projectid);
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		issues = issuesService.findById(id);
		return MAIN;
	}

	@Override
	public String save() throws Exception {
		if(milestoneid != null){
			issues.setMilestone(milestoneService.findById(milestoneid));
		}
		issues.setCreatetime(DateUtil.getNow());
		issues.setCreateuser(getCurrentUser());
		issues.setProject(projectService.findById(projectid));
		issues.setState(Const.ISSUES_STATE[0]);
		issuesService.save(issues);
		id = issues.getId();
		
		messageService.saveCreateIssues(issues, getCurrentUser());
		
		return RELOAD;
	}
	
	public String savetalk() throws Exception{
		talk.setCreatetime(DateUtil.getNow());
		talk.setIssues(issuesService.findById(id));
		talk.setUser(getCurrentUser());
		talkService.save(talk);
		return RELOAD;
	}
	
	public String changestate(){
		issues = issuesService.findById(id);
		issues.setState(state);
		issuesService.save(issues);
		id = issues.getId();
		
		messageService.saveChangeState(issues, getCurrentUser());
		
		return RELOAD;
	}
	
	public String givetask(){
		issues = issuesService.findById(id);
		issues.setWorkuser(userService.findById(userid));
		issuesService.save(issues);
		id = issues.getId();
		
		messageService.saveGiveTask(issues,getCurrentUser());
		
		return RELOAD;
	}

	
	
	//get and set
	public void setProject(Project project) {
		this.project = project;
	}

	public Project getProject() {
		return project;
	}

	public void setProjectid(Integer projectid) {
		this.projectid = projectid;
	}

	public Integer getProjectid() {
		return projectid;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getType() {
		return type;
	}

	public void setIssues(Issues issues) {
		this.issues = issues;
	}

	public Issues getIssues() {
		return issues;
	}

	public void setMilestoneid(Integer milestoneid) {
		this.milestoneid = milestoneid;
	}

	public Integer getMilestoneid() {
		return milestoneid;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setTalk(Talk talk) {
		this.talk = talk;
	}

	public Talk getTalk() {
		return talk;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getState() {
		return state;
	}

	public void setUserid(Integer userid) {
		this.userid = userid;
	}

	public Integer getUserid() {
		return userid;
	}

}
